using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerContorl : Singleton<PlayerContorl>
{
    [SerializeField]private Rigidbody2D myRb2d;
    [SerializeField]private Animator myAnim;
    public GameObject damagePanel;
    [Header("移动跳跃")]
    public int JumpNumber = 2;//可跳跃次数
    public float moveSpeed=5f;//移动速度
    public float JumpHead=5f;//跳跃
    public float moveDic;//摇杆输入
    public float JumpFoce;
    [Header("冲锋参数")]
    public float dashTime;//冲锋时间
    private float dashTimeLeft;//冲锋剩余时间
    private float lastDash=-10f;//上一次冲锋的时间点
    public float dashSpeed;//冲锋的速度
    public float dashCoolDown;//冲锋CD
    public Image cdImage;
    private Text cdText;
    [Header("攻击和打击感")]
    public float interval = 2f;
    public float shakeTime;//震动时间
    public int lightPause;//轻攻击停顿帧数
    public float lightStrength;//轻攻击震动强度
    public int heavyPause;//重攻击停顿帧数
    public float heavyStrength;//重攻击震动强度
    public float timer;
    public string attackType;//字符串用于区分轻重攻击
    public int comboStep=1;//动画序号
    public int AttackNumber=0;//攻击次数
    [Header("攻击补偿速度")]
    public float lightSpeed;
    public float heavySpeed;
    [Header("状态")]
    public bool isGround;
    public bool isDashing;
    public bool isAttack;
    public bool isShowHp=true;
    [Header("图层")]
    [SerializeField] private LayerMask layer;
    [SerializeField] private Vector3 check;

    /// <summary>
    /// 用于初始化
    /// </summary>
    void Start()
    {
        myRb2d = GetComponent<Rigidbody2D>();
        myAnim = GetComponent<Animator>();
        cdText = cdImage.transform.GetChild(0).GetComponent<Text>();
    }

    /// <summary>
    /// 用于处理物理相关的
    /// </summary>
    private void FixedUpdate()
    {

        if (myRb2d.velocity.x < 0)
            transform.localScale = new Vector3(-1, 1, 1);
        else if (myRb2d.velocity.x > 0)
            transform.localScale = new Vector3(1, 1, 1);
    }

    void Update()
    {
        isGround = Physics2D.OverlapCircle(transform.position + new Vector3(check.x, check.y, 0), check.z, layer);
        myAnim.SetFloat("Horizontal", moveDic);
        myAnim.SetFloat("Vertical", myRb2d.velocity.y);
        myAnim.SetBool("isGround", isGround);
        #region 攻击时间检测
        if (timer != 0)
        {
            timer -= Time.deltaTime;
            if (timer <= 0)
            {
                timer = 0;
                comboStep = 0;
            }
        }
        #endregion
        Dash();
        if (isDashing)
            return;
        Move();
        cdImage.fillAmount -= 1.0f / dashCoolDown *Time.deltaTime;
        CDShow();
    }
    void CDShow()
    {
        if (cdImage.fillAmount < 1)
        {
            cdText.gameObject.SetActive(true);
            cdText.text = (dashCoolDown * cdImage.fillAmount).ToString("f1");
        }
        if ((dashCoolDown * cdImage.fillAmount) == 0)
            cdText.gameObject.SetActive(false);
    }
    /// <summary>
    /// 按下跳跃按钮
    /// </summary>
    public void JumpButton()
    {
        if (isGround)
        {
            if (isAttack)
                AttackButton();
            myRb2d.velocity = new Vector2(0, JumpHead);
            myAnim.SetTrigger("Jump");
        }
    }
    public void DashButton()
    {
        if (Time.time >= (lastDash + dashCoolDown))
        {
            ReadyToDash();
        }
    }
 
    public void AttackButton()
    {
        if (!isAttack)
        {
            #region 轻攻击
            isAttack = true;//锁定攻击输入
            AttackNumber++;//攻击次数加一
            attackType = "Light";
            comboStep++;
            if (comboStep > 3)
                comboStep = 1;
            timer = interval;//开始计时
            //播放动画
            myAnim.SetTrigger("LightAttack");
            myAnim.SetInteger("ComboStep",comboStep);
            #endregion
            #region 重攻击
            if (AttackNumber >= 5)
            {//轻攻击次数大于3次切换重攻击
                attackType = "Heavy";
                timer = interval;//开始计时
                myAnim.SetTrigger("HeavyAttack");
                myAnim.SetInteger("ComboStep", 1);
                AttackNumber = 0;//重置攻击次数
            }
            #endregion
        }
    }
    /// <summary>
    /// 奔跑
    /// </summary>
    void Move()
    {
        if (isAttack)
            AttackButton();
        if (!isAttack)
            myRb2d.velocity = new Vector2(moveDic * moveSpeed, myRb2d.velocity.y);
        else
        {
            if (attackType == "Light")
                myRb2d.velocity = new Vector2(transform.localScale.x * lightSpeed, myRb2d.velocity.y);
            else if (attackType == "Heavy")
                myRb2d.velocity = new Vector2(transform.localScale.x * heavySpeed, myRb2d.velocity.y);
        }
    }
    /// <summary>
    /// 开始冲锋
    /// </summary>
    void ReadyToDash()
    {
        isDashing = true;
        dashTimeLeft = dashTime;
        lastDash = Time.time;
        cdImage.fillAmount = 1;
    }
    /// <summary>
    /// 冲锋
    /// </summary>
    void Dash()
    {
        if (isDashing)
        {
            if (dashTimeLeft>0)
            {
                if (myRb2d.velocity.y>0 && !isGround)
                {
                    myAnim.SetTrigger("Jump");
                    myRb2d.velocity = new Vector2(dashSpeed * transform.localScale.x, JumpFoce);
                }
                myRb2d.velocity = new Vector2(dashSpeed*transform.localScale.x,myRb2d.velocity.y);
                myAnim.SetFloat("Horizontal", dashSpeed * transform.localScale.x);
                dashTimeLeft -= Time.fixedDeltaTime;
                //在对象池取出残影
                PoolMgr.Instance().GetObj("Prefabs/PlayerDash", (o) =>
                {
                    GameObject go = o;
                    go.transform.position = this.transform.position;
                });
            }
            if (dashTimeLeft<=0)
            {
                isDashing = false;
                if (!isGround)
                {
                    myAnim.SetTrigger("Jump");
                    myRb2d.velocity = new Vector2(dashSpeed * transform.localScale.x, JumpFoce);
                }
            }
        }
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        float Attack = Random.Range(1,100);
        if (other.CompareTag("Enemy"))
        {
            if(attackType== "Light")
            {
                myAnim.speed = 0.5f;
            }
            else if(attackType == "Heavy")
            {
                myAnim.speed = 0.5f;
            }
            myAnim.speed = 1f;
            //敌人受伤
            if (transform.localScale.x > 0)
                other.GetComponent<Enemy>().GetHit(Vector2.right, Attack);
            else if (transform.localScale.x < 0)
                other.GetComponent<Enemy>().GetHit(Vector2.left, Attack);
            UIContorl uIContorl = Instantiate(damagePanel, other.transform.position, Quaternion.identity).GetComponent<UIContorl>();
            uIContorl.ShowUIDamage(Mathf.CeilToInt(Attack));
        }
    }
    /// <summary>
    /// 结束本次攻击
    /// </summary>
    public void AttackOver()
    {
        isAttack = false;
    }
    /// <summary>
    /// 移动
    /// </summary>
    /// <param name="dic"></param>
    public void Move(float dic)
    {
        moveDic = dic;
    }
    private void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(transform.position + new Vector3(check.x, check.y, 0), check.z);
    }
}
